// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System;
using GHIElectronics.NETMF.FEZ;
using LABSMFController.LABS.Classses;

namespace LABSMFController.LABS.Devices
{
    public static class ValveDevices
    {
        private static ValveDevice[] _valves;

        public static int MAXValves
        {
            get { return 3; }
        }

        public static ValveDevice[] Valves
        {
            get
            {
                if (_valves == null)
                    SetupValves();

                return _valves;
            }
            set { _valves = value; }
        }

        public static void ResetAllVavles()
        {
            SetupValves();

            for (int c = 0; c < MAXValves; c++)
            {
                Valves[c].TurnOff();
            }
        }


        public static void SetupValves()
        {
            if (_valves != null) return;

            Location.Setup();
            Valves = new ValveDevice[MAXValves];
            for (int c = 0; c < MAXValves; c++)
            {
                switch (c)
                {
                    case 0:
                        Valves[c] = new ValveDevice("Valve RO Into HLT " + c, FEZ_Components.Relay16.RelayChannel.Ch1);
                        break;
                        //case 1:
                        //    Valves[c] = new ValveDevice("Valve Into HLT Pot " + c, FEZ_Components.Relay16.RelayChannel.Ch2);
                        //    break;
                    case 1:
                        Valves[c] = new ValveDevice("Valve RO Into BK " + c, FEZ_Components.Relay16.RelayChannel.Ch3);
                        break;
                    case 2:
                        Valves[c] = new ValveDevice("Valve for Tap Water " + c, FEZ_Components.Relay16.RelayChannel.Ch4);
                        break;
                        //case 4:
                        //    Valves[c] = new ValveDevice("Valve Into BK Pot " + c, FEZ_Components.Relay16.RelayChannel.Ch5);
                        //    break;
                        //case 5:
                        //    Valves[c] = new ValveDevice("Valve To Fermenter " + c, FEZ_Components.Relay16.RelayChannel.Ch6);
                        //    break;
                        //case 6:
                        //    Valves[c] = new ValveDevice("Valve RO into BK " + c, FEZ_Components.Relay16.RelayChannel.Ch7);
                        //    break;
                        //case 7:
                        //    Valves[c] = new ValveDevice("Valve BK " + c, FEZ_Components.Relay16.RelayChannel.Ch8);
                        //    break;
                        //case 8:
                        //    Valves[c] = new ValveDevice("Valve MLT " + c, FEZ_Components.Relay16.RelayChannel.Ch9);
                        //    break;
                        //case 9:
                        //    Valves[c] = new ValveDevice("Valve Tap Water " + c, FEZ_Components.Relay16.RelayChannel.Ch10);
                        //    break;
                }
            }
        }

        public static void ValveOn(int valveNum)
        {
            Valves[valveNum].TurnOn();
        }


        public static void ValveOff(int valveNum)
        {
            Valves[valveNum].TurnOff();
        }

        public static string GetValvesClientOutPut()
        {
            string ret = "HEADER:Valves:" + Valves[0].Header + ";";
            for (int c = 0; c < MAXValves; c++)
            {
                ret = ret + "\r\n" + Valves[c].GetClientOutPut();
            }
            return ret;
        }
    }

    #region Nested type: ValveDevice

    public class ValveDevice
    {
        public ValveDevice(string description, FEZ_Components.Relay16.RelayChannel relayChannel)
        {
            Description = description;
            RelayNum = Convert.ToInt32(relayChannel.ToString());
            Relay = relayChannel;
            Header = "Description,RelayNum,IsOn";
        }

        public string Description { get; private set; }
        private FEZ_Components.Relay16.RelayChannel Relay { get; set; }
        public bool IsOn { get; private set; }
        public int RelayNum { get; private set; }
        public string Header { get; set; }

        public string GetClientOutPut()
        {
            string ret = Description + "," + RelayNum + "," + IsOn + ";";
            return ret;
        }

        public void TurnOn()
        {
            try
            {
                BeerProgram.TurnOnRelay(Relay);
            }
            catch
            {
                EventHandlers.InvokePanicTriggerEvent("Cannot turn on valve " + Description);
            }
            IsOn = true;
        }

        public void TurnOff()
        {
            try
            {
                BeerProgram.TurnOffRelay(Relay);
            }
            catch
            {
                EventHandlers.InvokePanicTriggerEvent("Cannot turn off valve " + Description);
            }
            IsOn = false;
        }
    }

    #endregion
}